<?php
/**
 * Created by PhpStorm.
 * Script Name: Minifollow.php
 * Create: 2023/1/11 9:08
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\tenant\controller;

use plugin\wechat\app\model\MiniFollow as FollowM;
use plugin\wechat\app\TenantController;
use Support\Request;

class Minifollow extends TenantController
{
    /**
     * @var FollowM
     */
    protected $model;

    public function __construct()
    {
        parent::__construct();
        $this->model = new FollowM();
    }

    /**
     * 粉丝列表
     * @param Request $request
     * @return mixed|\support\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function index(Request $request)
    {
        $mini_info = $request->miniInfo;
        if (request()->isPost()) {
            $post_data = input('post.');
            $where = [
                $this->tenantWhere(),
                ['mini_id', '=', $mini_info['id']]
            ];
            !empty($post_data['search_key']) && $where[] = [
                'openid', 'like', '%' . $post_data['search_key'] . '%'
            ];
            $query = $this->model->where($where);
            $total = $query->count();
            if ($total) {
                $list = $query->page($post_data['page'], $post_data['limit'])
                    ->order('id', 'desc')
                    ->select();
            } else {
                $list = [];
            }
            return $this->success('success', '', ['total' => $total, 'list' => $list]);
        }

        $builder = new ListBuilder();
        $builder->setSearch([
            ['type' => 'text', 'name' => 'search_key', 'title' => '关键词', 'placeholder' => '根据openid搜索']
        ])
            ->addTableColumn(['title' => 'openid', 'field' => 'openid',  'minWidth' => 100])
            ->addTableColumn(['title' => '手机号', 'field' => 'mobile', 'minWidth' => 100])
            ->addTableColumn(['title' => '首次访问', 'field' => 'create_time', 'minWidth' => 180]);

        return $builder->show();
    }
}